package com.hadron.media.slideshow.patterns.factory 
{
	import com.greensock.loading.LoaderMax;
	import com.hadron.media.slideshow.enum.SlideshowItemType;
	import com.hadron.media.slideshow.items.ISlideshowItem;
	import com.hadron.media.slideshow.items.SlideshowImageItem;
	import com.hadron.media.slideshow.items.SlideshowSwfItem;
	/**
	 * ...
	 * @author Hadron
	 */
	public class ItemCreator implements ICreator
	{
		private var imageExtenstions:Array = ['.jpg', '.jpeg', '.JPG', '.JPEG', '.gif', '.GIF', '.png', '.PNG', '.bmp', '.BMP'];
		private var videoExtensions:Array = ['.flv', '.FLV', '.mp4', '.MP4'];
		private var swfExtensions:Array = ['.swf', '.SWF'];
		private var extensionsArr:Array;
		private static var _instance:ItemCreator = new ItemCreator();
		
		public function ItemCreator() 
		{
			if (_instance != null)
			{
				throw new Error('This is kinda singleton!');
			}
			extensionsArr = [imageExtenstions, videoExtensions, swfExtensions];
		}
		
		public static function getInstance():ItemCreator
		{
			return _instance;
		}
		
		/* INTERFACE components.slideshow.ICreator */
		public function createItem(assetURL:String):ISlideshowItem
		{
			var item:ISlideshowItem;
			switch(getType(assetURL))
			{
				case SlideshowItemType.IMAGE:
				{
					item = new SlideshowImageItem();
					//set default behavior
					item.content = LoaderMax.getContent(assetURL);
					break;
				}
				
				/*case SlideshowItemType.VIDEO:
				{
					trace('creating video item');
					item = new SlideshowVideoItem(value);
					break;
				}*/
				
				case SlideshowItemType.SWF:
				{
					break;
				}
			}
			return item as ISlideshowItem;
		}
		
		private function getType(value:String):SlideshowItemType
		{			
			var curArr:Array;
			var curIndex:int;
			for (var i:int = 0; i < extensionsArr.length; i++) 
			{
				curArr = extensionsArr[i];
				for (var j:int = 0; j < curArr.length; j++) 
				{
					if (value.search(curArr[j]) != -1)
					curIndex = i;
					break;
				}
			}
			
			var type:SlideshowItemType;
			switch(curIndex) 
			{
				case 0:
					type = SlideshowItemType.IMAGE;
				break;
				
				case 1:
					type = SlideshowItemType.VIDEO;
				break;
				
				case 2:
					type = SlideshowItemType.SWF;
				break;
			}
			
			return type;
		}
	}
}